package customer.zspt.cn.customer.components.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.HorizontalScrollView;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import customer.zspt.cn.customer.Adapter.OrderAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.bean.OrderMsgDao;
import customer.zspt.cn.customer.utils.GetAllInfoUtils;
import customer.zspt.cn.customer.utils.SPUtils;
import customer.zspt.cn.customer.Widget.ViewPagerIndicator;

/**
 * Created by Administrator on 2018/6/19 0019.
 */

public class order extends Activity{
    private String[] title = new String[]{"我发起的","我受理的","未受理","已受理","已回复","已解决","已超时","已取消"};
    private List<List<OrderMsgDao.OrderMsg>> All = new ArrayList<>();
    @ViewInject(R.id.order_msg)
    private ViewPager order_msg;
    @ViewInject(R.id.order_nav)
    private ViewPagerIndicator order_nav;
    @ViewInject(R.id.scroll)
    private HorizontalScrollView scroll;
    private String guserid;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            OrderAdapter adapter = new OrderAdapter(title,getApplication(),All);
            order_msg.setAdapter(adapter);
            adapter.OnclickStartListener(new OrderAdapter.IonclickCallBack() {
                @Override
                public void onclick(String orderId, String ordercontent) {
                    Intent intent = new Intent(getApplication(),order_view.class);
                    intent.putExtra("orderId",orderId);
                    intent.putExtra("guserid",guserid);
                    intent.putExtra("ordercontent",ordercontent);
                    startActivity(intent);
                }
            });
            order_nav.setVp(order_msg,scroll);
            order_nav.setChildATitle(title,All);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
         guserid = getIntent().getStringExtra("userid");
        ViewUtils.inject(this);
        String token = SPUtils.getString(this, "token", "");
        GetAllInfoUtils.infoUtils.getFriendInfo(constant.AllMSG + "/order/list", token, this);
        GetAllInfoUtils.infoUtils.monitoringLinster(new GetAllInfoUtils.ReadyData() {
            @Override
            public void success(String data) {
                Log.e("data",data);
                pullData(data);
            }
            @Override
            public void fail() {

            }
        });
    }
    private void classification() {
        for(int i=0;i<title.length;i++)
        {
            List<OrderMsgDao.OrderMsg> obj = new ArrayList<>();
            All.add(obj);
        }
    }
    private void pullData(String data) {
        Gson gson = new Gson();
        OrderMsgDao orderMsgDao = gson.fromJson(data, OrderMsgDao.class);
        classification();
        if(orderMsgDao!=null)
        {
            List<OrderMsgDao.OrderMsg> ordermsg = orderMsgDao.data;
            for(int i=0;i<ordermsg.size();i++)
            {
                int index = ordermsg.get(i).orderStatus;
                All.get(index+2).add(ordermsg.get(i));

                if(guserid.equals(ordermsg.get(i).userid+""))
                {
                    All.get(0).add(ordermsg.get(i));
                }
            }
            mHandler.sendEmptyMessage(0);
        }
    }
}


